package com.equipment.fkdcn.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.equipment.fkdcn.pojo.Department;
import com.equipment.fkdcn.pojo.EParameter;
import com.equipment.fkdcn.pojo.EType;
import com.equipment.fkdcn.pojo.Params;
import com.equipment.fkdcn.service.ETypeServiceImpl;
import com.equipment.fkdcn.service.EparameterServiceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Field;
import java.util.*;

@Controller
@RequestMapping("/ep")
public class EParmeterController {

    //controller灞傝皟鐢╯ervice灞�
    @Autowired
    @Qualifier("eparameterService")
    private EparameterServiceImpl eparameterService;
    @Autowired
    @Qualifier("eTypeService")
    private ETypeServiceImpl eTypeService;

    //鏌ヨ鍒楄〃
    @GetMapping("/eList")
    public  String elist(Model model){
        List<EType> eTypes = eTypeService.AllQueryEType();
        model.addAttribute("eType",eTypes);
        return  "/eAdministration/elist";
    }

    //妯＄硦鏌ヨ
    @GetMapping("/params")
    @ResponseBody
    public  String likeParams(@RequestParam("params")String data){
        List<EParameter> list = eparameterService.allEParameter();
        List<JSONObject> jsons=new ArrayList<JSONObject>();
        List<JSONObject> newJson=new ArrayList<JSONObject>();
        for (EParameter eParameter:list) {
            JSONObject temp = new JSONObject();
            temp.put("eID",eParameter.getEid());
            temp.put("eName",eParameter.geteName());
            temp.put("model",eParameter.getModel());
            temp.put("etypeId", eParameter.getEtype().getId());
            temp.put("etype", eParameter.getEtype().getType());
            temp.put("brand", eParameter.getBrand());
            temp.put("manufacturer",eParameter.getManufacturer());
            temp.put("num",eParameter.getNum());
            temp.put("deliveryTime",eParameter.getDeliveryTime());
            temp.put("purchaseTime",eParameter.getPurchaseTime());
            jsons.add(temp);
        }
        if(data!=null&&!data.equalsIgnoreCase("")){
            for (int i=0;i<jsons.size();i++){
                JSONObject jsonObject = jsons.get(i);
                Set<String> keySet = jsonObject.keySet();
                Iterator<String> iterator = keySet.iterator();
                while (iterator.hasNext()){
                    String key=iterator.next().toString();
                    String temp = jsonObject.getString(key);
                    if (temp.indexOf(data)>=0){
                        newJson.add(jsons.get(i));
                        break;
                    }
                }
            }
            jsons=newJson;
        }


        String params= JSON.toJSONString(jsons);
        return params;
    }




    //鏀瑰彉璁惧椤甸潰
    @GetMapping("/modify")
    @ResponseBody
    public String pageModify(@RequestParam("parmeter")String parmeter, Model model) {
        JSONObject jsonObject = JSONObject.parseObject(parmeter);
        String id = jsonObject.getString("id");
        String ename = jsonObject.getString("ename");
        String typeId = jsonObject.getString("typeId");
        EParameter eParameter = new EParameter();
        eParameter.setEid(Integer.parseInt(id));
        eParameter.seteName(ename);
        List<EParameter> list = eparameterService.vagueQuery(eParameter);

       String  temp=null;
        if (list!=null){
            temp= JSON.toJSONString(list);

        }
        return temp;
    }




    //鏀瑰彉鏁版嵁
    @PostMapping("/temp")
    @ResponseBody
    public String modify(@RequestBody String data){
        JSONObject jsonObject = JSON.parseObject(data);
        EParameter eParameter = JSON.toJavaObject(jsonObject,EParameter.class);
        boolean flag = eparameterService.upEParameter(eParameter);
        return  "{\"state\":"+flag+"}";

    }



    @PostMapping("/addparam")
    @ResponseBody
    public String  addEParam(@RequestBody String data) {
        JSONObject jsonObject = JSON.parseObject(data);
        EParameter eParameter = JSON.toJavaObject(jsonObject, EParameter.class);
        boolean b = eparameterService.addEParameter(eParameter);
        return "{\"state\":" + b + "}";

    }

    //鍒犻櫎
    @GetMapping("/del")
    @ResponseBody
    public String del(@RequestParam("id") String id){
        boolean b = eparameterService.delEParameter(Integer.parseInt(id));
        return "{\"state\":"+b+"}";
    }



    //id查询
    @GetMapping("/idQuery")
    @ResponseBody
    public  String idQuery(@RequestParam("id")int id){
        EParameter eParameter = eparameterService.idQuery(id);
        String jsonString = JSON.toJSONString(eParameter);
        return jsonString;
    }


    
    //字段查询
    @GetMapping("/fielQuery")
    @ResponseBody
    public String fieldQuery(@RequestParam("params")String data) {
    	JSONObject parseObject = JSON.parseObject(data);
    	String eName = parseObject.getString("eName");
    	String typeId = parseObject.getString("typeId");
    	String dTime = parseObject.getString("dTime");
    	String pTime = parseObject.getString("pTime");
    	EParameter eParameter = new EParameter();
    	eParameter.seteName("%"+eName+"%");
    	EType eType = new EType();
    	eType.setId(Integer.parseInt(typeId));
    	eParameter.setEtype(eType);
    	eParameter.setDeliveryTime(dTime);
    	eParameter.setPurchaseTime(pTime);
    	List<EParameter> fieldQuery = eparameterService.fieldQuery(eParameter);
    	 List<JSONObject> newJson=new ArrayList<JSONObject>();
         for (EParameter e:fieldQuery) {
             JSONObject temp = new JSONObject();
             temp.put("eID",e.getEid());
             temp.put("eName",e.geteName());
             temp.put("model",e.getModel());
             temp.put("etypeId", e.getEtype().getId());
             temp.put("etype", e.getEtype().getType());
             temp.put("brand", e.getBrand());
             temp.put("manufacturer",e.getManufacturer());
             temp.put("num",e.getNum());
             temp.put("deliveryTime",e.getDeliveryTime());
             temp.put("purchaseTime",e.getPurchaseTime());
             newJson.add(temp);
         }
    	String jsonString = JSON.toJSONString(newJson);
    	return jsonString;
    }






}
